<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Config;
use think\Db;
use app\api\model\User as UserModel;
use think\db\Expression;
use app\common\library\Upload;

/**
 * 登录接口
 */
class Login extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 登录
     *
     */
    public function index()
    {
        $param = request()->param();
        $model = new UserModel;

        $user_id = $model->login($param);

        $token = $model->getToken();
        $data['token'] = $token;

        $this->success('请求成功', $data);
    }

    /**
     * 手机号登录
     *
     */
    public function getPhoneindex()
    {
        $param = request()->param();
        $model = new UserModel;

        $user_id = $model->loginindex($param);

        $token = $model->getToken();
        $data['token'] = $token;

        $this->success('请求成功', $data);
    }


    /**
     * 获取手机号
     *
     */
    public function getPhone()
    {
        $param = request()->param();
        $model = new UserModel;
        $User = $this->getUser();
        $phone = $model->wxphone($param, $User);
        $this->success('请求成功', $phone);
    }

    /**
     * 用户详情
     *
     */
    public function getUsers()
    {
        $param = request()->param();
        $type = empty($param['type'])?'':$param['type'];
        $User = $this->getUser($type);
        $this->success('查询成功', $User);
    }

    /**
     * 用户修改信息
     *
     */
    public function upUsers()
    {
        $param = request()->post();
        $User = $this->getUser();
        // 启动事务
        Db::startTrans();
        try {
            unset($param['token']);
            Db::name('wxuser')->where(['id' => $User['id']])->update($param);
            Db::commit();
            return json(['code' => 1, 'msg' => '请求成功']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 0, 'msg' => '失败' . $e->getMessage()]);
        }
    }

    /**
     * 绑定手机号
     *
     */
    public function upphone()
    {
        $param = request()->param();
        $User = $this->getUser();
        //判断当前微信是否绑定手机号
        if ($User['mobile']) {
            return json(['code' => 1, 'msg' => '请求成功1']);
        }
        // 启动事务
        Db::startTrans();
        try {
            unset($param['token']);
            //判断是否有当前的手机号注册
            $mobile_user = Db::name('wxuser_select')->where(['mobile' => $param['mobile']])->find();
            if ($mobile_user) {
                //判断是否绑定其他微信
                if ($mobile_user['openid']) {
                    return json(['code' => 0, 'msg' => '当前手机号已绑定其他微信，请用其他手机号绑定']);
                } else {
                    //修改手机号的openid，并且删除当前的数据
                    Db::name('wxuser_select')->where(['id' => $mobile_user['id']])->update(['openid' => $User['openid']]);
                    Db::name('wxuser_select')->where(['id' => $User['id']])->update(['deletetime' => time()]);
                }
            } else {
                Db::name('wxuser_select')->where(['id' => $User['id']])->update($param);
            }
            Db::commit();
            return json(['code' => 1, 'msg' => '请求成功']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 0, 'msg' => '失败' . $e->getMessage()]);
        }
    }

    /**
     * 公共配置信息
     *
     */
    public function info()
    {
        $site = Config::get('site');
        $this->success('查询成功', $site);
    }

}
